package com.liguo.whoisthespy.ui

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import club.ddkx.duoduokaixin.adapter.BaseAdapter
import com.liguo.whoisthespy.R
import com.liguo.whoisthespy.bean.LoadSerializableBean
import com.liguo.whoisthespy.bean.UserBean
import com.liguo.whoisthespy.bean.UserTbl
import com.liguo.whoisthespy.databinding.ItemSelectFirendsBinding
import com.liguo.whoisthespy.utils.save
import kotlinx.android.synthetic.main.activity_select_firends.*
import liguo.util.Util
import liguo.views.BaseActivity
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.onClick
import org.jetbrains.anko.toast

/**
 * 选择参与人员
 */
class SelectFirendsActivity : BaseActivity() {
    private val adapter by lazy {
        BaseAdapter<UserBean,ItemSelectFirendsBinding>(R.layout.item_select_firends, arrayListOf())
    }
    private var ids = 0
    private var num = 0
    override fun config(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_select_firends)
    }

    override fun initView(savedInstanceState: Bundle?) {
        val data = realm.copyFromRealm(realm.where(UserTbl::class.java).findAll())
        ids = data.count()
        asf_add.onClick {
            val nameStr = asf_edit.text.toString().trim()
            if(nameStr.isEmpty()){
                toast("请先填写名字")
                return@onClick
            }

            if(isRepeat(nameStr)){
                toast("已有该用户信息,不能重复添加")
                return@onClick
            }

            val bean = with(UserTbl()){
                id = ids
                name = nameStr
                this
            }
            save(bean,success = {
                adapter.addData(UserBean(bean))
                asf_edit.setText("")
                ids++
            })
        }
        asf_submit.onClick {
            if(num<4){
                toast("请先选择参与人员，最少4人")
            }else{
                num=0
                startActivity(intentFor<PhaseActivity>(Util.OPTION1 to with(LoadSerializableBean()){
                    val list = adapter.datas.filter { it.check }
                    list.forEach { it.check=false }
                    listUserBean = list
                    this
                }))
            }
        }
        asf_back.onClick { finish() }
        asf_recycler.layoutManager = GridLayoutManager(this,4)
        asf_recycler.adapter = adapter
        adapter.clearAddAllData(data.map { UserBean(it) })
        adapter.onBind { itemBingding, position, data ->
            itemBingding.root.onClick {
                data.check = !data.check
                if(data.check)  num++ else num--
                asf_txt_yixuan.text ="$num 名选手"
                asf_recycler.adapter.notifyDataSetChanged()
            }
        }
    }

    override fun initData() {
    }

    /**
     * 判断是否有重复的用户
     */
    fun isRepeat(s:String):Boolean{
        adapter.datas.forEach {
            if(it.user.name == s){
                return true
            }
        }
        return false
    }

    override fun onResume() {
        super.onResume()
        adapter.notifyDataSetChanged()
    }
}
